package getsmart.project.database;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class UsingDatabase {
	public static Object[] getData() {
		String db_url = "http://192.168.1.17:1991/GetSmart/listPelajaran.php";
		InputStream is = null;
		String line = null;
		ArrayList<NameValuePair> request = new ArrayList<NameValuePair>();
		String bssid = "";
		request.add(new BasicNameValuePair("bssid", bssid));
		Object returnValue[] = new Object[4];
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpContext localContext = new BasicHttpContext();
			HttpPost httppost = new HttpPost(db_url);
			httppost.setEntity(new UrlEncodedFormEntity(request));
			HttpResponse response = httpclient.execute(httppost, localContext);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			Log.e("log_tag", "Error in http connection" + e.toString());
		}
		String result = "";
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();

			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			result = sb.toString();
		} catch (Exception e) {
			Log.e("log_tag", "Error in http connection" + e.toString());
		}
		try {
			JSONArray jArray = new JSONArray(result);
			JSONObject json_data = jArray.getJSONObject(0);
			returnValue[0] = (json_data.getString("building"));
			returnValue[1] = (json_data.getString("floor"));
			returnValue[2] = (json_data.getString("lon"));
			returnValue[3] = (json_data.getString("lat"));

		} catch (JSONException e) {
			Log.e("log_tag", "Error parsing data" + e.toString());
		}
		return returnValue;
	}
}
